#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
#endregion

namespace Runnergame
{

    public class Mastercontroller : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Model.Player player;
        Model.Level level;
        View.Camera camera;
        KeyboardState currentKeyboardState;
        KeyboardState oldKeyboardState;

        public Mastercontroller()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            
            camera = new View.Camera(GraphicsDevice.Viewport);
            level = new Model.Level(camera);
            player = new Model.Player(level);
            currentKeyboardState = new KeyboardState();


            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player.LoadContent(this.Content);
            level.LoadContent(this.Content);

        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {

            CheckInput();
            player.Update(gameTime, level);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            level.Draw(spriteBatch, camera);
            player.Draw(spriteBatch, player, camera);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void CheckInput()
        {
            currentKeyboardState = Keyboard.GetState();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                //player.playerPosition.X = player.playerPosition.X + player.playerSpeed;
                player.velocity.X = 0.1f;
                //camera.CameraMovement(player.playerPosition.X);
            }
            else if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                //player.playerPosition.X = player.playerPosition.X - player.playerSpeed;
                player.velocity.X = -0.1f;
                //camera.CameraMovement(player.playerPosition.X);
            }
            else
            {
                player.velocity.X = 0; 
            }
            if (currentKeyboardState.IsKeyDown(Keys.Space) && currentKeyboardState != oldKeyboardState)
            {
                player.jump();
            }

            oldKeyboardState = currentKeyboardState;
        }
    }
}
